engines.all().map((ScriptEngine) => {
  if (engines.myEngine().toString() !== ScriptEngine.toString()) {
    ScriptEngine.forceStop();
  }
});
// 监听通知栏消息, 监听到消息打开对应的qq群, 获取关键词对应的消息, 发送消息
auto();
events.observeNotification();
let groupMap = {
  群名: "群号xxxxxxxxx",
  牙叔教程二群: "582976637",
};
let baseUrl = "http://192.168.5.8:3001/group-msg";
events.onNotification(function (notification) {
  log(notification);
  if (notification.getPackageName() === "com.tencent.mobileqq") {
    log("收到QQ消息");
    let obj = {
      title: notification.getTitle(),
      text: notification.text,
      when: notification.when,
      tickerText: notification.tickerText,
      packageName: notification.getPackageName(),
    };
    notification.delete();
    //         { title: '牙叔教程二群 (7条新消息)',
    //   text: '☾ 分寸 ☽: 输入法弹出还行，1秒:左右，输入:文字后缩进要8秒',
    //   when: 1666862029027,
    //   tickerText: '☾ 分寸 ☽(牙叔教程二群):输入法弹出还行，1秒左右，输入文字后缩进要8秒',
    //   packageName: 'com.tencent.mobileqq' }
    log(obj);
    if (!obj.title) {
      log("title 不存在");
      return false;
    }
    // 如果text里面包含 自省致知, 就不发送消息
    if (obj.text.indexOf("自省致知") !== -1) {
      log("自省致知");
      return false;
    }
    let groupName = obj.title.split(" ")[0];
    let groupId = groupMap[groupName];
    // 第一个冒号后的所有内容都是message
    if (!obj.tickerText) {
      log("tickerText 不存在");
      return false;
    }
    // 消息前缀
    let prefix = obj.tickerText.split(":")[0];
    // 前缀替换为空
    let message = obj.tickerText.replace(prefix, "");
    // 第一个冒号替换为空
    message = message.replace(":", "");
    // 去掉前后空格
    message = message.trim();

    /* -------------------------------------------------------------------------- */
    if (groupId && groupName && message) {
      postData({
        groupId: groupId,
        groupName: groupName,
        message: message,
      });
    }
  }
});

/* -------------------------------------------------------------------------- */
function postData(data) {
  log("客户端发送的数据: " + JSON.stringify(data));
  http.postJson(baseUrl, data, {}, function (res, err) {
    if (err) {
      console.error(err);
      return;
    }
    log("code = " + res.statusCode);
    log("html = " + res.body.string());
  });
}
function printObj(obj) {
  var arr = [];
  for (var k in obj) {
    arr.push(k);
  }
  arr.sort();
  console.log(arr);
}
/* -------------------------------------------------------------------------- */
/*
let 按下 = `0003 003a 00000001
0003 0035 000001F4
0003 0036 000003E8
0000 0002 00000000
0001 014a 00000001
0000 0000 00000000`;
let 抬起 = `0003 0035 000001F4
0003 0036 000003E8
0000 0002 00000000
0001 014a 00000000
0000 0000 00000000`;
let lines = 按下 + "\n" + 抬起;
let adbCheck = $shell.checkAccess("adb");
if (!adbCheck) {
  console.log("没有adb权限, 请先使用shizuku激活autojs的adb权限");
  exit();
} else {
  console.log("已有adb权限");
}
$shell.setDefaultOptions({ adb: true });

lines = lines.split("\n");
var len = lines.length;
let cmds = [];
for (var i = 0; i < len; i++) {
  let line = lines[i];
  line = line.replace(/.*event5: /, "");
  let arr = line.split(" ");
  let type = arr[0];
  let code = arr[1];
  let value = arr[2];
  // 转十进制
  type = parseInt(type, 16);
  code = parseInt(code, 16);
  value = parseInt(value, 16);
  let cmd = "sendevent /dev/input/event5 " + type + " " + code + " " + value;
  cmds.push(cmd);
}
*/
// setInterval(() => {
//     // 一次性执行多条命令
//     let res = cmds.join(" && ");
//     $shell(res);
// }, 1000 * 60);
/* -------------------------------------------------------------------------- */
let packageName = "org.autojs.autojspro";
setInterval(() => {
  app.launchPackage(packageName);
  setTimeout(() => {
    home();
  }, 3000);
}, 1000 * 10);
/* -------------------------------------------------------------------------- */
threads.start(function () {
  //传递第三个参数为true以循环播放音乐
  let audioPath = "/sdcard/脚本/noSound.mp3";
  media.playMusic(audioPath, 1, true);
});
